{{extend '../_layouts/home.html'}}

{{block 'title'}}{{'多人博客 - 首页'}}{{/block}}

{{block 'body'}}
<section class="container">
  {{include '../_partials/settings-nav.html'}}
  <div class="col-md-5">
    <div class="subhead">
      <h2>修改密码</h2>
    </div>
    <hr>
    <form id="update_pwd">
      <div class="form-group">
        <label for="exampleInputPassword1">当前密码</label>
        <input type="password" name="password" class="form-control" id="password" placeholder="">
      </div>
      <div class="form-group">
        <label for="exampleInputPassword1">新的密码</label>
        <input type="password" name="newpassword" class="form-control" id="newpassword" placeholder="">
      </div>
      <div class="form-group">
        <label for="exampleInputPassword1">确认密码</label>
        <input type="password" name="confirm" class="form-control" id="confirm" placeholder="">
      </div>
      <button type="submit" class="btn btn-success">保存</button>
    </form>
    <div class="subhead">
      <h2>注销账号</h2>
      <hr>
    </div>
    <div>
      <p>一旦注销，不可找回，请谨慎操作</p>
      <button class="btn btn-danger" id="delete">删除</button>
    </div>
    <hr>
  </div>
</section>
{{/block}}
{{block 'script'}}
<script>
//改变导航状态
$("[href*=admin]").addClass('active');
$("[href*=profile]").removeClass('active');
//更新密码
$('#update_pwd').on('submit', function (e) {
      e.preventDefault()
      var formData = $(this).serialize()
      $.ajax({
        url: '/updatepwd',
        type: 'post',
        data: formData,
        dataType: 'json',
        success: function (data) {
          var err_code = data.err_code
          if (err_code === 0) {
            // window.alert('注册成功！')
            // 服务端重定向针对异步请求无效
            $(".modal-content .modal-body").html(data.message);
            $("#myModal").trigger('click');
            window.setTimeout(function(){
              window.location.href = '/logout'
            }, 2000);
          } else if (err_code === 1) {
            $(".modal-content .modal-body").html(data.message);
            $("#myModal").trigger('click');
          } else if (err_code === 2) {
            $(".modal-content .modal-body").html(data.message);
            $("#myModal").trigger('click');
          } else if (err_code === 500) {
            window.alert('服务器忙，请稍后重试！')
          }
        }
      })
    })
$('#delete').on('click', function(e){
  $(".modal-content .modal-body").html(`
  <strong>您确定要注销账号码？<strong><br/><br/>
  <p>
    <button class="btn btn-danger" id="#confirm-del" data-dismiss="modal">确定注销</button>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <button class="btn btn-primary" data-dismiss="modal">按 错 了</button>   
  </p>
  `);
  $("#myModal").trigger('click');
});

//把点击确定注销的事件，注删到modal上
$("#myModal").next('.modal').delegate('button', 'click',function(e){
  //点击的是 注销按钮，发的注销清求
  if($(this).attr('id') === '#confirm-del'){
    $.ajax({
      url: '/delete',
      type: 'post',
      dataType: 'json',
      success: function(data){
        var err_code = data.err_code
        if (err_code === 0) {
          $(".modal-content .modal-body").html(data.message);
          $("#myModal").trigger('click');
          window.setTimeout(function(){
              window.location.href = '/logout'
          }, 2000);
        } else if ( err_code === 1 ){
          $(".modal-content .modal-body").html(data.message);
          $("#myModal").trigger('click');
        }else if( err_code === 2){
          $(".modal-content .modal-body").html(data.message);
          $("#myModal").trigger('click');
        }
      }
    })
  }
  
})

</script>
{{/block}}
